#include <Arduino.h>

// put function declarations here:
int row_pin[] = {9, 8, 7, 6};
int col_pin[] = {5, 4, 3, 2};
char keys[4][4] = {
    {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'},
    {'7', '8', '9', 'C'},
    {'*', '0', '#', 'D'}};

void setup()
{
  // put your setup code here, to run once:
  for (int i = 0; i < sizeof(row_pin) / sizeof(row_pin[0]); i++)
  {
    pinMode(row_pin[i], OUTPUT);
    digitalWrite(row_pin[i], HIGH);
  }

  for (int i = 0; i < sizeof(col_pin) / sizeof(col_pin[0]); i++)
  {
    pinMode(col_pin[i], INPUT);
    digitalWrite(col_pin[i], HIGH);
  }

  Serial.begin(9600);
}

void loop()
{
  // put your main code here, to run repeatedly:
  for (int row = 0; row < sizeof(row_pin) / sizeof(row_pin[0]); row++)
  {
    digitalWrite(row_pin[row], LOW);
    for (int col = 0; col < sizeof(col_pin) / sizeof(col_pin[0]); col++)
    {
      if (digitalRead(col_pin[col]) == LOW)
      {
        Serial.print("key:(");
        Serial.print(row + 1);
        Serial.print(",");
        Serial.print(col + 1);
        Serial.print("),");
        Serial.println(keys[row][col]);
        while (digitalRead(col_pin[col]) == LOW)
        {
          delay(1);
        }
        break;
      }
    }
    digitalWrite(row_pin[row], HIGH);
  }
}

// put function definitions here: